/*
 * @lc app=leetcode.cn id=15 lang=typescript
 *
 * [15] 三数之和
 */

// @lc code=start
function threeSum(nums: number[]): number[][] {
    if (nums.length < 3) {
        return [];
    }
    const size = nums.length;
    const arr = nums.sort((a, b) => a - b);
    console.log(nums)
    if (arr[0] > 0 || arr[arr.length - 1] < 0) {
        return [];
    }
    const res: any = [];
    for (let i = 0; i < size; i++) {
        if (arr[i] > 0) {
            console.log(res)
            return res;
        }
        if (i > 0 && arr[i] === arr[i - 1]) {
            continue;
        }
        let h = i + 1;
        let l = size - 1;
        console.log(arr[i], arr[h], arr[l])
        while (h < l) {
            const temp = arr[i] + arr[h] + arr[l];
            if (temp > 0) {
                l--;
            }
            if (temp < 0) {
                h++;
            }
            if (temp === 0) {
                res.push([arr[i], arr[h], arr[l]]);
                while (h < l && arr[h] === arr[h + 1]) {
                    h++;
                }
                while (h < l && arr[l] === arr[l - 1]) {
                    l--;
                }
                h++;
                l--;
            }
        }
    }

    console.log(res);
    return res;
}
threeSum([1, -1, -1, 0]);
// @lc code=end
